package code.tools.commponents
{
	import code.structure.manager.loader.DllLoaderManager;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	/**
	 *遮罩文本 
	 * @author liangqian
	 * 
	 */	
	public class ShadowTextField extends Component
	{
		private var textTf:TextField;
		private var textShadow:Sprite;
		private var textBm:Bitmap;
		
		private var _filters:Array;
		/** 遮罩的初始高度  */
		private var _shadowInitHeight:Number;
	
		private var _wid:int;
		private var _align:String;
		public function ShadowTextField(parent:DisplayObjectContainer,xpos:Number,ypos:Number,
						shadowKey:String,shadowSubKey:String,filters:Array,align:String,
						textWidth:int,textColor:uint,textFont:String,textSize:int)
		{
			_filters = filters;
			_wid = textWidth;
			_align = align;
			super(parent,xpos,ypos);
			mouseEnabled = mouseChildren = false;
			textTf = Helpers.FormatTextField(null,0,0,textWidth,TextFieldAutoSize.LEFT,textColor,textFont,textSize);
			textTf.wordWrap = true;
			DllLoaderManager.getInstance().getMaterial(shadowKey,shadowSubKey,shadowLoadCompleteHandler);
		}
		
		private function shadowLoadCompleteHandler(c:*,key:String = ''):void
		{
			textShadow = Helpers.drawBmd(c,textShadow);
			textShadow.width = _wid;
			_shadowInitHeight = textShadow.height;
			addChild(textShadow);
			refreshText();
		}
		
		public function set text(t:String):void
		{
			textTf.text = t;
			if(textShadow)
				refreshText();
		}
		
		private function refreshText():void
		{
			textShadow.height = textTf.numLines * _shadowInitHeight;
			textBm = Helpers.drawConvertBm(textBm,textTf);
			textBm.cacheAsBitmap = true;
			textShadow.filters = _filters;
			switch(_align)
			{
				case TextFieldAutoSize.CENTER:
					textBm.x = (wid - textTf.textWidth)*.5;
					break;
				case TextFieldAutoSize.RIGHT:
					textBm.x = wid - textTf.textWidth;
					break;
			}
			addChild(textBm);
			textShadow.mask = textBm;
		}

		public function get wid():int
		{
			return _wid;
		}

		public function set wid(value:int):void
		{
			_wid = value;
		}

	}
}